﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;

namespace ET.Monitor.DAL
{
    public class MailSendDAL
    {
        //TODO 修改邮件服务器信息
        string mailServer = "smtp.etsolar.com", strFrom = "etom@etsolar.com",
               userName = "etom@etsolar.com", password = "a1s2d3!@#";

        public bool SendMail(string toAddress,string ccAddress,string title,string body)
        {
            MailAddress from = new MailAddress(strFrom, "ET全球运维中心");
            MailMessage message = new MailMessage();
            message.From = from;
            if (string.IsNullOrEmpty(toAddress.Trim()))
            {
                return false;
            }
            message.To.Add(toAddress);
            if (!string.IsNullOrEmpty(ccAddress.Trim()))
            {
                message.CC.Add(ccAddress);
            }
            message.Subject = title;
            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            SmtpClient client = new SmtpClient(mailServer);
            try
            {
                client.UseDefaultCredentials = true;
                client.Credentials = new NetworkCredential(userName, password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SendMail(string toAddress, string ccAddress, string title, string body,string fileName)
        {
            MailAddress from = new MailAddress(strFrom, "ET全球运维中心");
            MailMessage message = new MailMessage();
            message.From = from;
            if (string.IsNullOrEmpty(toAddress.Trim()))
            {
                return false;
            }
            message.To.Add(toAddress);
            if (!string.IsNullOrEmpty(ccAddress.Trim()))
            {
                message.CC.Add(ccAddress);
            }
            if (!string.IsNullOrEmpty(fileName.Trim()))
            {
                Attachment att=new Attachment(fileName);
                message.Attachments.Add(att);
            }
            message.Subject = title;
            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            SmtpClient client = new SmtpClient(mailServer);
            try
            {
                client.UseDefaultCredentials = true;
                client.Credentials = new NetworkCredential(userName, password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
